/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import book.PhoneBookItem;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import worker.Collector;

/**
 *
 * @author x3m
 */
public class indexBean extends MainBean {

    private PhoneBookItem[] pages = null;
    
    private void delete() {
        String[] chk_arr = request.getParameterValues("delcheck");
        Collector.deleteItems(chk_arr);
    }

    public String getUsersTable() {
        StringBuilder code = new StringBuilder();
        code.append("<table class='tbl_main' border = '0' width='100%'>\n");

        code.append("<tr>");

        code.append("<td>");
        code.append("ФИО");
        code.append("</td>");

        code.append("<td>");
        code.append("Телефон");
        code.append("</td>");

        code.append("<td>");
        code.append("Адресс");
        code.append("</td>");

        code.append("<td>");
        code.append("");
        code.append("</td>");

        code.append("<td>");
        code.append("");
        code.append("</td>");

        code.append("</tr>");
        for (int i = 0; i < pages.length; i++) {
            code.append("<tr class='tbl_row' id='"+pages[i].getPrivateID()+"'>");

            code.append("<td>");
            code.append(pages[i].getFullName());
            code.append("</td>");

            code.append("<td>");
            code.append(pages[i].getAllPhones());
            code.append("</td>");

            code.append("<td>");
            code.append(pages[i].getAddr());
            code.append("</td>");

            code.append("<td>");
            code.append("<a href='edit.jsp?action=edit&id=").append(pages[i].getPrivateID()).append("'><img alt='удалить' src='images/Modify.ico' /></a>");
            code.append("</td>");

            code.append("<td>");
            code.append("<input type='checkbox' onClick='selectCheckbox();' name='delcheck' value='").append(pages[i].getPrivateID()).append("' />");
            code.append("</td>");

            code.append("</tr>\n");

        }

        code.append("<td colspan='4'>");
        code.append("<a href='edit.jsp?action=add'>Добавить пользователя</a>");
        code.append("</td>");

        code.append("<td>");
        code.append("<input id='BTN_DELETE' disabled='disabled' type='submit' value='Удалить' onClick=\"deleteRows();\" />");
        code.append("</td>");

        code.append("</table>\n");

        return code.toString();
    }

    @Override
    public boolean isLoad() {
        if ("true".equals(getStringFromParam("doDelete"))) {
            //throw new UnsupportedOperationException("Not supported yet.");
            delete();
        }
        
        if (!"".equals(getStringFromParam("generate"))) {
            String count = getStringFromParam("generate");
            Collector.generateUsers(Integer.parseInt(count));
            try {
                response.sendRedirect("index.jsp");
                //Collector.generateUsers(1);
            } catch (IOException ex) {
                Logger.getLogger(indexBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }        
        
        pages = Collector.getAll();
        return true;
    }
}
